package com.atguigu.mybatisplus;

public class Test1{
    public static void main(String[] args) throws Exception {
        Business business = new Business();
        for(int q = 0;q<5;q++) {

            new Thread(() -> {
                try {
                    for (int i = 0; i < 2; i++) {
                        business.decr();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "子线程2").start();
            new Thread(() -> {
                try {
                    for (int i = 0; i < 2; i++) {
                        business.incr();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "子线程1").start();
            //主线程
                business.all();

        }
    }
}
class Business{
    public int num = 0;
    public int a = 0;

    public synchronized void incr() throws Exception{
        //判断
        if(num!=0){
            this.wait();

        }
        //干活
        System.out.println(Thread.currentThread().getName()+":\t"+num);
        num++;
        a++;
        //通知
        this.notify();
    }

    public synchronized void decr() throws Exception {
        if(num==0){
            this.wait();

        }
        System.out.println(Thread.currentThread().getName()+":\t"+num);
        num--;
        a++;
        this.notify();
    }
    public synchronized void all() throws Exception{
        //判断
        if(a!=4){
            this.wait();
        }
        //干活
        for (int i =0;i<3;i++) {
            System.out.println(Thread.currentThread().getName() + ":\t" + "我是主线程");
        }
        //通知
        a=0;

        this.notify();
    }
}
